package filter;

import entity.User;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter({"/article/create",
        "/article/delete",
        "/article/modify",
        "/user/delete"
})
public class LoginFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //用户未登录，将请求打回登录页面
        HttpServletRequest request = ( HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("login_user");
        if (loginUser==null){
            response.sendRedirect("/login");
        }else {
            chain.doFilter(req, resp);
        }
    }
}
